﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class MainCam : MonoBehaviour {

	[SerializeField]
	ShadowCam m_shadowCamCtrl;

	[SerializeField]
	Camera m_shadowCam;

	[SerializeField]
	Material[] RSMMaterials;

	void OnPreRender() {
		Shader.DisableKeyword("ShadowPass");
		Shader.EnableKeyword("ShadingPass");

		var MatLight = m_shadowCam.transform.worldToLocalMatrix;
		var MatLightProj = m_shadowCam.projectionMatrix;

		foreach (var mtl in RSMMaterials) {
			mtl.SetTexture("MapDepth", m_shadowCamCtrl.RSMBuffers[0]);
			mtl.SetTexture("MapWPos", m_shadowCamCtrl.RSMBuffers[1]);
			mtl.SetTexture("MapWNom", m_shadowCamCtrl.RSMBuffers[2]);
			mtl.SetTexture("MapFlux", m_shadowCamCtrl.RSMBuffers[3]);
			mtl.SetMatrix(nameof(MatLight), MatLight);
			mtl.SetMatrix(nameof(MatLightProj), MatLightProj);
		}
	}
}